package minijvm.engine;

import java.util.HashMap;
import java.util.Map;

/**
 * 在这里所有对象，都是JavaObject
 * 模拟java堆中的 java对象
 * @author ChangLiang
 * @date 2020/4/27
 */
public class JavaObject {

    public static final int OBJECT = 1;
    public static final int STRING = 2;
    public static final int INT = 3;

    int type;

    private String className;

    private String stringValue;

    private int intValue;

    /**
     * 模拟在java堆中的对象中保存 name="andy" and age=29 这样的结构
     */
    private Map<String, JavaObject> fieldValues = new HashMap<>();

    /**
     *
     * @param fieldName     name    or  age
     * @param fieldValue    "andy"  or  29
     */
    public void setFieldValue(String fieldName, JavaObject fieldValue) {
        fieldValues.put(fieldName, fieldValue);
    }

    public JavaObject getFieldValue(String fieldName) {
        return this.fieldValues.get(fieldName);
    }

    public JavaObject(int type) {
        this.type = type;
    }

    public String getClassName() {
        return className;
    }

    public void setClassName(String className) {
        this.className = className;
    }

    public void setStringValue(String stringValue) {
        this.stringValue = stringValue;
    }

    public void setIntValue(int intValue) {
        this.intValue = intValue;
    }

    public int getIntValue() {
        return intValue;
    }

    public int getType() {
        return type;
    }

    @Override
    public String toString() {
        switch (this.getType()) {
            case INT:{
                return String.valueOf(this.intValue);
            }
            case STRING:{
                return this.stringValue;
            }
            case OBJECT:{
                return this.className + ":" + this.fieldValues;
            }
            default:{
                return null;
            }
        }
    }
}
